//
//  HomeHDTypeCell.m
//  jingdongfang
//
//  Created by LJL on 2018/5/9.
//  Copyright © 2018年 BOE. All rights reserved.
//

#import "HomeHDTypeCell.h"
#import "HomeHDTypeVC.h"

@implementation HomeHDTypeCell

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor c3Color];
        [self loadUI];
    }
    return self;
}

- (void)loadUI
{
    for (int i = 0; i < 11; i++)
    {
        CGFloat leftSpa = 10;
        CGFloat btnSpa = 5;
//        CGFloat top = 0;
//        CGFloat left = 10;
        
        CGFloat btnWid = (KSCREENWIDTH - 35) / 4;
        CGFloat FirstBtnHei = (KSCREENWIDTH - 35) / 4 * 15 / 17;
        CGFloat btnHei = (KSCREENWIDTH - 35) / 4 * 7 / 17;
        CGSize firstBtnSize = CGSizeMake((KSCREENWIDTH - 35) / 4, (KSCREENWIDTH - 35) / 4 * 15 / 17);
        CGSize btnSize = CGSizeMake((KSCREENWIDTH - 35) / 4, (KSCREENWIDTH - 35) / 4 * 7 / 17) ;
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.backgroundColor = [UIColor c2Color];
        btn.layer.cornerRadius = 2.5f;
        btn.layer.masksToBounds = YES;
        btn.tag = 1000 + i;
        [self addSubview:btn];

        if (i == 0)
        {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(leftSpa);
                make.top.mas_equalTo(0);
                make.size.mas_equalTo(firstBtnSize);
            }];
        } else if ( 0 < i && i < 7)
        {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo(leftSpa + btnWid + btnSpa + (btnWid + btnSpa) * ((i - 1) % 3));
                make.top.mas_equalTo((btnHei + btnSpa) * ((i - 1) / 3));
                make.size.mas_equalTo(btnSize);
            }];
        } else {
            [btn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.mas_equalTo((btnWid + btnSpa) * ((i - 1) % 4) + leftSpa);
                make.top.mas_equalTo(FirstBtnHei + btnSpa);
                make.size.mas_equalTo(btnSize);
            }];
        }
        @KWeakObj(btn);
        [btn.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(0);
            make.centerY.mas_equalTo(btnWeak);
            make.height.mas_equalTo(btnHei);
        }];
        [btn setTitleColor:[UIColor c4Color] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:16];
        btn.titleLabel.textAlignment = NSTextAlignmentCenter;
        [btn addTarget:self action:@selector(typeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(void)typeBtnClick:(UIButton *)btn
{
    HomeHDTypeVC *vc = [[HomeHDTypeVC alloc] init];
    vc.title = _typeArr[btn.tag - 1000][@"label"];
    vc.type = [NSString stringWithFormat:@"%@",_typeArr[btn.tag - 1000][@"id"]];
    [UIViewController ig_pushViewController:vc animated:YES];
}

-(void)setTypeArr:(NSArray *)typeArr
{
    if (_typeArr != typeArr)
    {
        _typeArr = typeArr;
        for (int i = 0; i < typeArr.count; i++)
        {
            UIButton *btn = (UIButton *)[self viewWithTag:1000 + i];
            [btn setTitle:typeArr[i][@"label"] forState:UIControlStateNormal];
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    for (UIButton *btn in self.subviews)
    {
        if (btn.tag == 1000)
        {
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btn.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = btn.bounds;
            maskLayer.path = maskPath.CGPath;
            btn.layer.mask = maskLayer;
        }
        else if (btn.tag == 1003)
        {
            UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:btn.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(10, 10)];
            CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
            maskLayer.frame = btn.bounds;
            maskLayer.path = maskPath.CGPath;
            btn.layer.mask = maskLayer;
        }
    }
}

@end
